package com.randomrpg.game.world

import java.io.File
import java.io.FileOutputStream
import java.io.ObjectOutputStream
import java.io.FileInputStream
import java.io.ObjectInputStream

object Saver {

  private val ext = ".rpgsave"
  
  def saveGame(player: Player, map: GameMap) {
    val fos = new FileOutputStream(player.name + ext)
    val oos = new ObjectOutputStream(fos)
    
    oos.writeObject(player)
    oos.writeObject(map)
  }
  
  def loadGame(name: String): (Player, GameMap) = {
    val fis = new FileInputStream(name + ext)
    val ois = new ObjectInputStream(fis)
    
    var player = ois.readObject.asInstanceOf[Player]
    var map = ois.readObject.asInstanceOf[GameMap]
    (player, map)
  }
  
  def listSaves: List[String] = {
    var list = List[String]()
    new File(".").listFiles.foreach { file =>
      if(file.getName.matches(".*" + ext))
        list = file.getName.replaceAll(ext, "") :: list
    }
    list
  }
}